﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using EShop.Model;
using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseClassEdit : frmBase
    {
        delegate void WriteToControlEventHandler(object sender, OnWirteTocontrolArgs e);
        private string strMerchandiseClassID = string.Empty;
        private bool boolIsChanged = false;
        public frmMerchandiseClassEdit(string strMerchandiseClassID)
        {
            InitializeComponent();
            this.strMerchandiseClassID = strMerchandiseClassID;
        }
        WriteToControlEventHandler OnWriteToControl;
        private void frmMerchandiseClassEdit_Load(object sender, EventArgs e)
        {
            lblMechandiseClassID.Text = strMerchandiseClassID;
            Thread t = new Thread(new ThreadStart(GetMerchandiseClassInfomation));
            t.IsBackground = true;
            t.Start();
        }
        /// <summary>
        /// 获取商品类型信息
        /// </summary>
        private void GetMerchandiseClassInfomation()
        {
            Thread.Sleep(10);
            EShop.BLL.MerchandiseClassBLL oMerchandiseClassBLL = new EShop.BLL.MerchandiseClassBLL();
            OnWriteToControl = new WriteToControlEventHandler(OnWriteToControlEvent);
            DataTable dt = oMerchandiseClassBLL.QueryMerchandiseClass(this.strMerchandiseClassID);
            string strMerchandiseClassName = dt.Rows[0]["MerchandiseClassName"].ToString();
            string strMerchandiseClassResume = dt.Rows[0]["MerchandiseClassResume"].ToString();
            string strMercahndiseClassRemark = dt.Rows[0]["MerchandiseClassRemark"].ToString();
            this.BeginInvoke(OnWriteToControl, new object(), new OnWirteTocontrolArgs(txtMerchandiseClassName, strMerchandiseClassName));
            Thread.Sleep(10);
            this.BeginInvoke(OnWriteToControl, new object(), new OnWirteTocontrolArgs(rtbMerchandiseResume, strMerchandiseClassResume));
            Thread.Sleep(10);
            this.BeginInvoke(OnWriteToControl, new object(), new OnWirteTocontrolArgs(rtbMerchandiseRemark, strMercahndiseClassRemark));
        }

        /// <summary>
        /// 将数据异步写入到控件上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWriteToControlEvent(object sender, OnWirteTocontrolArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(OnWriteToControl, sender, e);
            }
            else
            {
                e.control.Text = e.Msg;
                boolIsChanged = false;
            }
        }

        private void txtMerchandiseClassName_TextChanged(object sender, EventArgs e)
        {
            this.boolIsChanged = true;
        }

        private void rtbMerchandiseResume_TextChanged(object sender, EventArgs e)
        {
            this.boolIsChanged = true;
        }

        private void rtbMerchandiseRemark_TextChanged(object sender, EventArgs e)
        {
            this.boolIsChanged = true;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (boolIsChanged)
            {
                if (string.IsNullOrEmpty(txtMerchandiseClassName.Text.Trim()))
                {
                    MessageBox.Show("请输入商品类型名称");
                }
                EShop.Model.MerchandiseClass oMerchandiseClass = new EShop.Model.MerchandiseClass();
                EShop.BLL.MerchandiseClassBLL oMerchandiseClassBLL = new EShop.BLL.MerchandiseClassBLL();
                oMerchandiseClass.MerchandiseClassID = Convert.ToInt32(lblMechandiseClassID.Text);
                oMerchandiseClass.MerchandiseClassName = SystemLibrary.SQLQuoteName((txtMerchandiseClassName.Text.Trim()));
                oMerchandiseClass.MerchandiseClassResume = SystemLibrary.SQLQuoteName(rtbMerchandiseResume.Text);
                oMerchandiseClass.MerchandiseClassRemark = SystemLibrary.SQLQuoteName(rtbMerchandiseRemark.Text);
                if (oMerchandiseClassBLL.UpdateMerchandiseClass(oMerchandiseClass))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            if (boolIsChanged)
            {
                string msg = "您已经将数据进行了修改，是否更新到服务器？";
                if (MessageBox.Show(msg, "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    this.btnSubmit_Click(sender, e);
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }


    }
    internal class OnWirteTocontrolArgs : System.EventArgs
    {
        private Control _control;

        public Control control
        {
            get { return _control; }
            set { _control = value; }
        }

        private string _msg;

        public string Msg
        {
            get { return _msg; }
            set { _msg = value; }
        }

        public OnWirteTocontrolArgs(Control _control, string _msg)
        {
            this._control = _control;
            this._msg = _msg;
        }
    }
}
